package com.vdimitr.happycube.puzzle.piece.match;

import com.vdimitr.happycube.puzzle.piece.PuzzlePiece;
import com.vdimitr.happycube.puzzle.piece.construction.PuzzlePieceTransformer;

/**
 * This class is responsible for matching a puzzle piece with each puzzle piece
 * given in a row using a left to right matching strategy.
 * 
 * @see PuzzlePieceLeftToRightMatcher
 * 
 * @author vasileios.dimitriadis
 * 
 */
public class PuzzlePieceLeftToRowMatcher {

	/**
	 * The matching strategy to use for determining if the puzzle pieces match
	 */
	private PuzzlePieceLeftToRightMatcher matcher = new PuzzlePieceLeftToRightMatcher();

	/**
	 * The transformer to use for rotating the puzzle piece to match with the
	 * row
	 */
	private PuzzlePieceTransformer transformer = new PuzzlePieceTransformer();

	/**
	 * Determines if the puzzle piece's right side fits together with each
	 * puzzle piece's left side in a left-to-right manner, starting from the
	 * first puzzle piece and moving right while rotating clockwise each time
	 * 
	 * @param puzzlePiece
	 *            the puzzle piece to fit
	 * @param puzzlePieces
	 *            a series of puzzle pieces
	 * @return True iff the given puzzle piece matches with all other puzzle
	 *         pieces
	 */
	public boolean puzzlePiecesMatchLeftToRow(PuzzlePiece puzzlePiece,
			PuzzlePiece[] puzzlePieces) {
		PuzzlePiece leftPuzzlePiece = new PuzzlePiece(puzzlePiece);
		boolean result = true;
		for (int i = 0; i < puzzlePieces.length; i++) {
			PuzzlePiece rightPuzzlePiece = puzzlePieces[i];
			result = result
					&& matcher.puzzlePiecesMatchLeftToRight(leftPuzzlePiece,
							rightPuzzlePiece);
			leftPuzzlePiece = transformer
					.rotateCounterClockwiseOnce(leftPuzzlePiece);
		}
		return result;
	}
}
